package cn.kk.springboot01.demo.controller;

import cn.kk.springboot01.demo.entity.Emp;
import cn.kk.springboot01.demo.service.EmpService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/*
 *@author KK on 2022.10.09 17:23
 */
@Controller
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpService empService;
    @GetMapping("/getAll")
    public String getAll(Model model){
        List<Emp> emps =  empService.getAll();
        model.addAttribute("emps",emps);
        return "index";
    }

    @RequestMapping("/login")
    public String login(String ename,String pwd,HttpSession session){
        Emp emp = empService.getEmpByname(ename);
        session.setAttribute("emp",emp);
        return "index";
    }
    @RequestMapping("/delEmp")
    public String delEmp(Integer eno,Model model){
        int i = empService.delEmp(eno);
        String msg = "删除成功";
        if (i>0) {
            model.addAttribute("msg",msg);
            return "msg";
        }
        else {
            msg = "删除失败";
            model.addAttribute("msg",msg);
            return "msg";
        }
    }
}
